package org.RSG.Application;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.*;

public class BMP
{
	String pxl[];
	BufferedReader buffer;
	DataOutputStream wbuf;
	int pos = 0;
	//int clearcolour = 0;
	
	public BMP(String pix[], int width, int height)
	{
		this.pxl = pix;
		String file = showSaveWindow();
		if(file == "")
		{
			System.out.println("File Save Terminated");
			return;
		}
		this.writeImage(file, width, height);
		System.out.println("Save Finished!");
	}
	
	public void writeImage(String filepath, int width, int height)
	{
		try
		{
			this.wbuf = new DataOutputStream(new FileOutputStream(filepath));
			
			this.wbuf.write((byte)66); //B
			this.wbuf.write((byte)77); //M
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //FileSize
			this.write2((byte)0, (byte)0); //Reserved
			this.write2((byte)0, (byte)0); //Reserved
			this.wbuf.write((byte)54); //Header Offset
			this.write4((byte)0, (byte)0, (byte)0, (byte)40); //Bytes in Structure(Not sure what this is)
			this.owrite4(width); //Width
			this.owrite4(height); //Height
			this.write4((byte)0, (byte)0, (byte)0, (byte)1); //Planes
			this.write2((byte)0, (byte)24); //Bitcount
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Compression
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Size Image
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Pels X
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Pels Y
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Palette
			this.write4((byte)0, (byte)0, (byte)0, (byte)0); //Important
			this.wbuf.write((byte)0); //Fix(Not sure what happened missing one bit somehow)
			
			for(int i = height - 1; i >= 0; i--)
			{
				int z;
				for(z = 0; z < width; z++)
				{
					//Decipher Tile Data
					int red = Integer.parseInt(this.pxl[this.getTileID(z, i)].substring(0, 3));
					int green = Integer.parseInt(this.pxl[this.getTileID(z, i)].substring(3, 6));
					int blue = Integer.parseInt(this.pxl[this.getTileID(z, i)].substring(6, 9));
					//int alpha = Integer.parseInt(this.pxl[this.getTileID(z, i)].substring(9, 12));
					//System.out.println("Pixel(" + i + ", " + z + "): " + red + ", " + blue + "" + green + "; TID: " + this.getTileID(z, i));
					this.wbuf.writeByte(blue); //Blue
					this.wbuf.writeByte(green); //Green
					this.wbuf.writeByte(red); //Red
				}
				//Write Padding
				if(width % 4 > 0)
				{
					for(int j = 0; j < (width % 4); j++)
					{
						this.wbuf.write((byte)0);
					}
				}
			}
			
			
			this.wbuf.close();
		} catch(IOException e) {
			System.out.println("An IOException has occured while Writing BMP File!");
		}
	}
	
	public String showSaveWindow()
	{
		JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.dir")));
		chooser.showSaveDialog(chooser);
		String filepath = chooser.getSelectedFile().getAbsolutePath();
		if(!filepath.substring(filepath.length()-4, filepath.length()).equalsIgnoreCase(".bmp"))
		{
			filepath += ".BMP";
		}
		if(new File(filepath).exists())
		{
			int res = JOptionPane.showConfirmDialog(new JButton(), "File Already Exists! Are you sure you want to overwrite?", "RainSquared Owl", JOptionPane.OK_CANCEL_OPTION);
			System.out.println(res);
			if(res != 0)
			{
				return "";
			}
		}
		return filepath;
	}
	//TODO: Fix
	public int getTileID(int x, int y)
	{
		int tileid = y * Application.inFocus.width;
		tileid += x;

		return tileid;
	}
	
	private void write4(byte one, byte two, byte three, byte four) throws IOException
	{
		this.wbuf.write(one);
		this.wbuf.write(two);
		this.wbuf.write(three);
		this.wbuf.write(four);
	}
	
	/*
	private void write4(int number) throws IOException
	{
		this.wbuf.write(number & 255);
		this.wbuf.write((number >> 8) & 255);
		this.wbuf.write((number >> 16) & 255);
		this.wbuf.write((number >> 24) & 255);
	}
	*/
	
	private void owrite4(int number) throws IOException
	{
		this.wbuf.write(number >> 24);
		this.wbuf.write(number >> 16);
		this.wbuf.write(number >> 8);
		this.wbuf.write(number);
	}
	
	private void write2(byte one, byte two) throws IOException
	{
		this.wbuf.write(one);
		this.wbuf.write(two);
	}
}